import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
//ElementPlus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
//VXETable
import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css'
//css
import "@/assets/css/reset.css";
import "@/assets/iconfont/iconfont.css";
import '@/assets/iconfont/iconfont.js'
import '@/assets/iconfont/iconfont.ttf'
import '@/assets/iconfont/iconfont.woff'

//引入lodash
import _ from "lodash";
//引入jquery
import $ from "jquery"
//动态路由
import finalRoutes from "@/router/permissionRoutes";
import Cookies from 'js-cookie'

finalRoutes.forEach(function (item) {
  router.addRoute(item)
})
//mock ！！！ 开启mock 导出会出问题
// require("./mock/index");
//使用VXETable
router.beforeEach((to, from, next)=>{
  if (to.path === "/login") Cookies.remove('csrftoken')

  // 路由跳转之前，中止还在等待的请求
  const axiosCancels = store.state.axiosCancels;
  if (axiosCancels.length) {
    // @ts-ignore
    axiosCancels.forEach(cancel => cancel && cancel());
    store.commit("SET_AXIOS_CANCELS", []);
  }
  next()
})
const app = createApp(App, {productionTip: true});
app.use(store)
.use(router)
.use(ElementPlus)
.use(VXETable)
.use(_)
.use($)
.mount('#app')
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// @ts-ignore
// 注册全局变量
import {__uiConfig} from "../public/config/uiConfig"
Object.keys(__uiConfig).forEach(item => {
  app.config.globalProperties[item] = __uiConfig[item];
})


app.directive('permission', {
  mounted(el, binding) {
    const permissionValue = binding.value // 获取传入的权限值
    if (!hasPermission(permissionValue)) {
      el.style.display = 'none' // 如果没有该权限则将元素设置为不可见
    } else {
      el.removeAttribute('style') // 否则移除之前添加的样式属性
    }
  },
})

function hasPermission(permissionValue:any) {
  // 根据实际情况判断当前用户是否具有相应的权限
  return true; // 这里只作为示例返回true表示有权限
}

console.log('全局引入jquery:',$)
console.log('全局引入lodash:',_)
console.log('app', app)
